package ca.eloas.judging.rest.resources;

import ca.eloas.judging.model.CompetitionObject;
import ca.eloas.judging.model.Judge;
import ca.eloas.judging.model.JudgeObject;
import ca.eloas.judging.model.ScoreObject;
import ca.eloas.judging.model.TeamObject;
import ca.eloas.judging.rest.repositories.CompetitionRepository;
import ca.eloas.judging.rest.repositories.JudgeRepository;
import ca.eloas.judging.rest.repositories.ScoreRepository;
import ca.eloas.judging.rest.repositories.TeamRepository;
import ca.eloas.restsupport.utils.BasicResource;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author JP
 */
@Path("/debug")
public class PopulateResource extends BasicResource {

    @Inject
    CompetitionRepository competitions;

    @Inject
    TeamRepository teams;

    @Inject
    JudgeRepository judges;

    @Inject
    ScoreRepository scores;

    @GET
    @Path("/populate")
    public Object populate() {

        CompetitionObject compet = new CompetitionObject();
        compet.setName("Expo-Sciences 2013");

        competitions.deleteAll(competitions.iterateAll());
        competitions.save(compet);

        teams.deleteAll(teams.iterateAll());
        judges.deleteAll(judges.iterateAll());
        scores.deleteAll(scores.iterateAll());

        List<JudgeObject> existingJudges = new ArrayList<JudgeObject>();
        for(int a = 0; a < 20; a ++) {

            JudgeObject jo = new JudgeObject("test" + a + "@test.com");
            jo.setPassword("judge" + a);
            jo.judgeCompetiton(compet);
            judges.save(jo);
            existingJudges.add(jo);
        }

        for(int a = 0; a < 20; a ++) {

            TeamObject to = compet.createTeam("Equipe " + a);
            teams.save(to);
            System.err.println("team " + to.getKey());
            for(int j = 0; j < 3 ; j ++ ) {

                JudgeObject jo = existingJudges.get(new Random().nextInt(20));
                ScoreObject s = to.score(jo, new Random().nextInt(100), "judge " + jo.getName() + " says so to " + to.getId());
                scores.save(s);

                System.err.println("Added score from " + jo.getId() + " to " + to.getId());
            }
        }

        return null;
    }
}
